/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.model;

import com.zh.jbet.core.entity.GameContext;
import com.zh.jbet.core.service.TreeService;
import java.util.ArrayList;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.tree.WicketTreeModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author sonrisa
 */
public class GameContextTreeModel extends WicketTreeModel {

    @SpringBean(name = "GameContextService")
    private TreeService<GameContext> gameContextService;
    
    private ListModel<DefaultMutableTreeNode> markedToDelete;

    public GameContextTreeModel(GameContext context) {
        InjectorHolder.getInjector().inject(this);
        DefaultMutableTreeNode root;
        if (context.getId() == null) {
            //gameContextService.save(context);
            root = new DefaultMutableTreeNode(context);
        } else {
            root = gameContextService.getRootNodeById(context.getId());
        }
        setObject(new DefaultTreeModel(root));
        this.markedToDelete = new ListModel<DefaultMutableTreeNode>(new ArrayList<DefaultMutableTreeNode>());
    }


    @Override
    public void detach() {
        super.detach();
        //markedToDelete.detach();
    }

    public void save() {
        boolean rootDeleted = false;
        for (DefaultMutableTreeNode d: markedToDelete.getObject()) {
            if (getObject().getRoot().equals(d)) {
                rootDeleted = true;
            }
            gameContextService.deleteNode(d);
        }
        if (!rootDeleted) {
            gameContextService.saveRootNode((DefaultMutableTreeNode)getObject().getRoot());
        }
    }

    public void reload() {
        markedToDelete.getObject().clear();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)getObject().getRoot();
        root = gameContextService.getRootNodeById(((GameContext)root.getUserObject()).getId());
        ((DefaultTreeModel)getObject()).setRoot(root);
    }
    
    public void markToDelete(DefaultMutableTreeNode node) {
        if (((GameContext)node.getUserObject()).getId() != null) {
            markedToDelete.getObject().add(node);
        }
    }    
}
